{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Tutorial for using Prompt flow and sending traces to Arize AI"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Import arize_otel and other dependencies required for setup."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "from arize_otel import Endpoints, register_otel\n",
    "from opentelemetry.sdk.environment_variables import OTEL_EXPORTER_OTLP_ENDPOINT\n",
    "from promptflow.tracing._start_trace import setup_exporter_from_environ"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set up OpenTelemetry using our convenience function: register_otel with your Arize credentials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setup OTEL via our convenience function\n",
    "register_otel(\n",
    "    endpoints=Endpoints.ARIZE,\n",
    "    space_key=\"INSERT_YOUR_ARIZE_SPACE_KEY_HERE\",  # in app space settings page\n",
    "    api_key=\"INSERT_YOUR_ARIZE_API_KEY_HERE\",  # in app space settings page\n",
    "    model_id=\"test-prompt-flow-chat\",  # name this to whatever you would like\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then set up the OpenTelemetry endpoint to be Arize and use Prompt flow's setup_exporter_from_environ to start tracing any further flows and LLM calls."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[OTEL_EXPORTER_OTLP_ENDPOINT] = Endpoints.ARIZE\n",
    "setup_exporter_from_environ()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Open and read flow.py which contains ChatFlow, the flow we are using in this example."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"flow.py\") as fin:\n",
    "    print(fin.read())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create your Azure OpenAI Connection and follow the link in the comments if you need to create an account. If not, paste your credentials where specified."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from promptflow.client import PFClient\n",
    "from promptflow.connections import AzureOpenAIConnection\n",
    "\n",
    "# client can help manage your runs and connections.\n",
    "pf = PFClient()\n",
    "\n",
    "# To create an Azure OpenAI resource, Follow:\n",
    "# https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/create-resource?pivots=web-portal\n",
    "connection = AzureOpenAIConnection(\n",
    "    name=\"open_ai_connection\",\n",
    "    api_key=\"INSERT_YOUR_API_KEY_HERE\",\n",
    "    api_base=\"INSERT_YOUR_API_BASE_HERE\",\n",
    "    api_type=\"azure\",\n",
    ")\n",
    "\n",
    "conn = pf.connections.create_or_update(connection)\n",
    "print(\"successfully created connection\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create your config with a specified model to be used in flow calls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from promptflow.core import AzureOpenAIModelConfiguration\n",
    "\n",
    "# create the model config to be used in below flow calls\n",
    "config = AzureOpenAIModelConfiguration(connection=\"open_ai_connection\", azure_deployment=\"gpt-4o\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create your Chat Flow and see the traces rendered in Arize!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from flow import ChatFlow\n",
    "\n",
    "# create a chatFlow obj with connection\n",
    "chat_flow = ChatFlow(config)\n",
    "# run the flow as function, which will be recorded in the trace\n",
    "result = chat_flow(question=\"What is ChatGPT? Please explain with concise statement\")\n",
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "promptflow-git-books-test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
